/***************************************************************************\
 * Copyright (C) by Keio University
 * Tools.cuh created in 01 2012.
 * Mail : fdesorbi@hvrl.ics.keio.ac.jp
 *
 * Tools.cuh is part of the HVRL Engine Library.
 *
 * The HVRL Engine Library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * The HVRL Engine Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 \***************************************************************************/

#ifndef GPGPU_CUDA_TOOLS_CUH__
#define GPGPU_CUDA_TOOLS_CUH__


#include <cuda.h>
#include <cuda_runtime_api.h>

#include <cutil.h>
#include <cutil_math.h>

namespace hvrl
{

  namespace cuda
  {


    __forceinline__ __device__ uint
    rgbaFloatToInt(float4 rgba)
    {
      rgba.x = __saturatef(fabs(rgba.x)); // clamp to [0.0, 1.0]
      rgba.y = __saturatef(fabs(rgba.y));
      rgba.z = __saturatef(fabs(rgba.z));
      rgba.w = __saturatef(fabs(rgba.w));
      return (uint(rgba.w * 255.0f) << 24) | (uint(rgba.z * 255.0f) << 16)
          | (uint(rgba.y * 255.0f) << 8) | uint(rgba.x * 255.0f);
    }

    __forceinline__ __device__ float4
    rgbaIntToFloat(uint c)
    {
      float4 rgba;
      rgba.x = (c & 0xff) * 0.003921568627f; //  /255.0f;
      rgba.y = ((c >> 8) & 0xff) * 0.003921568627f; //  /255.0f;
      rgba.z = ((c >> 16) & 0xff) * 0.003921568627f; //  /255.0f;
      rgba.w = ((c >> 24) & 0xff) * 0.003921568627f; //  /255.0f;
      return rgba;
    }

  }

}

#endif
